﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using System.Text;

namespace PeisongWeb
{
    public partial class seller_submitPriceList : customerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (allowAccess(Convert.ToInt32(companyType.Provider)))
                {
                    string dtString=logic.webset.list().UpdatePriceDt;
                    TimeSpan span = TimeSpan.Parse(dtString);
                    lblTime.Text = string.Format("{0}:{1}", span.Hours,span.Minutes.ToString().PadLeft(2,'0'));
                    DropDownlistBind();
                    DropDownChildBind(0);
                    rptBind();
                   
                }
                else
                {
                    Library.Script.ClientUrl("index.html");
                    return;
                }
            }
        }
        void DropDownlistBind()
        {
            DataTable dt = DAL.SellerRange.sellerRaneParentCategory(logic.customer.companyId);
            if (dt.Rows.Count > 0)
            {
                DropParentCategory.DataSource = dt;
                DropParentCategory.DataBind();
            }
            DropParentCategory.Items.Insert(0,new ListItem("-无-","0"));
        }

        void DropDownChildBind(int ParentId)
        {
            DropChildCategory.Items.Clear();
            DataTable dt = DAL.SellerRange.sellerRangeChildCategory(logic.customer.companyId, ParentId);
            if (dt.Rows.Count > 0)
            {
                DropChildCategory .DataSource = dt;
                DropChildCategory.DataBind();
            }
            DropChildCategory.Items.Insert(0, new ListItem("-无-", "0"));
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            if (DropParentCategory.SelectedItem.Value != "0")
                where.AppendFormat(" and parentId={0}", DropParentCategory.SelectedItem.Value);
            if (DropChildCategory.SelectedItem.Value != "0")
                where.AppendFormat(" and categoryId={0}", DropChildCategory.SelectedItem.Value);
            if (txt_productName.Value != "")
                where.AppendFormat(" and productName LIKE '%{0}%'", txt_productName.Value.Trim());
            DataSet ds = logic.sellerRange.sellerRangelistWithPrice(logic.customer.companyId,where.ToString(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void rpt_list_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType== ListItemType.Item || e.Item.ItemType== ListItemType.AlternatingItem)
            {
                DataRowView rowView = e.Item.DataItem as DataRowView;
                int productId=Convert.ToInt32(rowView["productId"]);
                Repeater rpt = e.Item.FindControl("rpt_price_list") as Repeater;

                DataTable dt = logic.price.listWithBusinessNatures(productId, logic.customer.companyId);
                rpt.DataSource = dt;
                rpt.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void btn_select_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = -1;
            rptBind();
        }

        protected void DropParentCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectIndex=0;
            int.TryParse(DropParentCategory.SelectedValue,out selectIndex);
            DropDownChildBind(selectIndex);
        }
    }
}
